package com.mall.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.pageResult.PageResult;
import com.mall.pojo.UmsMenu;
import com.mall.service.UmsMenuService;
import com.mall.utils.Result;
import com.mall.vo.UmsMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/menu")
public class UmsMenuController {

    @Autowired
    private UmsMenuService umsMenuService;

    //添加后台菜单
    @PostMapping("/create")
    public Result<UmsMenu> create(@RequestBody UmsMenu umsMenu) {
        umsMenuService.create(umsMenu);
        return Result.success(null, "操作成功");
    }

    //根据ID删除后台菜单
    @PostMapping("delete/{id}")
    public Result<UmsMenu> delete(@PathVariable("id") Long id) {
        umsMenuService.delete(id);
        return Result.success(null, "操作成功");
    }

    //分页查询后台菜单
    @GetMapping("/list/{parentId}")
    public Result<PageResult<UmsMenu>> list(@PathVariable("parentId") Long parentId,
                                            @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                            @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        Page<UmsMenu> page = umsMenuService.list(parentId, pageNum, pageSize);
        PageResult<UmsMenu> pageResult = new PageResult<>();
        pageResult.setPageNum(pageNum);
        pageResult.setPageSize(pageSize);
        if (CollUtil.isEmpty(page.getRecords())) {
            pageResult.setList(new ArrayList<>());
            return Result.success(pageResult, "操作成功");
        }
        pageResult.setTotal(Convert.toInt(page.getTotal()));
        pageResult.setTotalPage(Convert.toInt(page.getPages()));
        pageResult.setList(page.getRecords());
        return Result.success(pageResult, "操作成功");
    }

    //todo 树形结构返回所有菜单列表
    @GetMapping("/treeList")
    public Result<List<UmsMenuVo>> treeList(){
        List<UmsMenuVo> result = umsMenuService.treeList();
        return Result.success(result,"操作成功");
    }

    //修改后台菜单
    @PostMapping("/update/{id}")
    public Result update(@PathVariable("id") Long id,
                         @RequestBody UmsMenu umsMenu) {
        umsMenuService.update(id, umsMenu);
        return Result.success(null, "操作成功");
    }

    //修改菜单显示状态
    @PostMapping("/updateHidden/{id}")
    public Result updateHidden(@PathVariable("id") Long id,
                               @RequestParam("hidden") Integer hidden) {
        umsMenuService.updateHidden(id, hidden);
        return Result.success(null, "操作成功");
    }

    //根据ID获取菜单详情
    @GetMapping("/{id}")
    public Result<UmsMenu> selectById(@PathVariable("id") Long id) {
        UmsMenu umsMenu = umsMenuService.selectById(id);
        return Result.success(umsMenu, "操作成功");
    }

}
